{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 0.矩阵的初等变换与线性方程组\n",
    "## 0.1.环境、库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import numpy as np\n",
    "import sympy as sp\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import itertools\n",
    "from functools import reduce\n",
    "plt.rcParams['font.sans-serif'] = ['SimHei'] #指定默认字体   \n",
    "plt.rcParams['axes.unicode_minus'] = False #解决保存图像是负号'-'显示为方块的问题\n",
    "x, y, z, t = sp.symbols('x y z t')\n",
    "k, m, n = sp.symbols('k m n', integer=True)\n",
    "f, g, h = sp.symbols('f g h', cls=sp.Function)\n",
    "sp.init_printing()\n",
    "# common tool function\n",
    "def getM(var_name,r,c):\n",
    "    \"\"\"\n",
    "    make a sympy s matrix\n",
    "    :param var_name:the symbol name\n",
    "    :param r:matrix s rows\n",
    "    :param c:matrix s cols\n",
    "    \"\"\"\n",
    "    if r>1 and c>1:\n",
    "        ss=sp.symbols(\"{0}_{{{{0:{1}}}{{0:{2}}}}}\".format(var_name,r,c))        \n",
    "    elif r==1:\n",
    "        ss=sp.symbols(\"{0}_{{0:{1}}}\".format(var_name,c))\n",
    "    elif c==1:\n",
    "        ss=sp.symbols(\"{0}_{{0:{1}}}\".format(var_name,r))\n",
    "    else:\n",
    "        raise Exception('invalid input parameter！')\n",
    "    return sp.Matrix([ss[i*c:(i+1)*c] for i in range(r)])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 0.2.回顾:克拉默法则\n",
    "线性方程组：<br>\n",
    "$\\begin{cases} a_{11}x_{1}+\\dots+a_{1n}x_{n}=b_{1}\\\\\\dots\\\\  a_{n1}x_{1}+\\dots+a_{nn}x_{n}=b_{n}\\end{cases}$<br>\n",
    "\n",
    ">**定理4**：如果上述线性方程的系数行列式不等于零，则该线性方程组一定有解，而且解是唯一的。<br>\n",
    "**定理$4^{'}$**：如果线性方程组无解或有两个不同的解，则它的系数行列式必为零。\n",
    "\n",
    "用克拉默法则解线性方程组的两个条件：\n",
    ">1. 方程个数等于未知量个数；\n",
    "2. 系数行列式不等于零。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1.矩阵的初等变换\n",
    "## 1.1.初等变换的概念\n",
    "**例:**求解线性方程组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left ( \\left[\\begin{matrix}2 & -1 & -1 & 1\\\\1 & 1 & -2 & 1\\\\4 & -6 & 2 & -2\\\\3 & 6 & -9 & 7\\end{matrix}\\right], \\quad \\left[\\begin{matrix}x_{0}\\\\x_{1}\\\\x_{2}\\\\x_{3}\\end{matrix}\\right], \\quad \\left[\\begin{matrix}2\\\\4\\\\4\\\\9\\end{matrix}\\right]\\right )$$"
      ],
      "text/plain": [
       "⎛⎡2  -1  -1  1 ⎤  ⎡x_{0}⎤  ⎡2⎤⎞\n",
       "⎜⎢             ⎥  ⎢     ⎥  ⎢ ⎥⎟\n",
       "⎜⎢1  1   -2  1 ⎥  ⎢x_{1}⎥  ⎢4⎥⎟\n",
       "⎜⎢             ⎥, ⎢     ⎥, ⎢ ⎥⎟\n",
       "⎜⎢4  -6  2   -2⎥  ⎢x_{2}⎥  ⎢4⎥⎟\n",
       "⎜⎢             ⎥  ⎢     ⎥  ⎢ ⎥⎟\n",
       "⎝⎣3  6   -9  7 ⎦  ⎣x_{3}⎦  ⎣9⎦⎠"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xa=sp.Matrix([[2,-1,-1,1],[1,1,-2,1],[4,-6,2,-2],[3,6,-9,7]])\n",
    "xx=getM('x',4,1)\n",
    "xb=sp.Matrix([2,4,4,9])\n",
    "xa,xx,xb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}2 x_{0} - x_{1} - x_{2} + x_{3}\\\\x_{0} + x_{1} - 2 x_{2} + x_{3}\\\\4 x_{0} - 6 x_{1} + 2 x_{2} - 2 x_{3}\\\\3 x_{0} + 6 x_{1} - 9 x_{2} + 7 x_{3}\\end{matrix}\\right] = \\left[\\begin{matrix}2\\\\4\\\\4\\\\9\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡   2⋅x_{0} - x_{1} - x_{2} + x_{3}   ⎤   ⎡2⎤\n",
       "⎢                                     ⎥   ⎢ ⎥\n",
       "⎢   x_{0} + x_{1} - 2⋅x_{2} + x_{3}   ⎥   ⎢4⎥\n",
       "⎢                                     ⎥ = ⎢ ⎥\n",
       "⎢4⋅x_{0} - 6⋅x_{1} + 2⋅x_{2} - 2⋅x_{3}⎥   ⎢4⎥\n",
       "⎢                                     ⎥   ⎢ ⎥\n",
       "⎣3⋅x_{0} + 6⋅x_{1} - 9⋅x_{2} + 7⋅x_{3}⎦   ⎣9⎦"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp.Eq(xa*xx,xb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAA4AAAASCAYAAABrXO8xAAAABHNCSVQICAgIfAhkiAAAAM5JREFUOI3N0rEyQ0EUxvEfJcMVWhoZ8Qpq75PnoDJ0Kl5BK0U67yAUiUYmpZGYoRDNuePOkXszYxS+5ps95/x3Z3c//lC7uMIz3jHCOVpNUBsTzHGDU/RjfY+dOvA2hrqpfhb1y0XQfjSHWE29DUwxw7o0cBzew2cCX3GHNRxl8DD8oeYaj+GdDBbhLzVgWd/K4DKthM8zWO5YWKzN6lwVHIR3asCD8B9v0Lb8O97Ed2T9KgDlqdXInfiO3EBD5GAP1xjjA0+4wHYT9I/1BRgnMa7SDNsAAAAAAElFTkSuQmCC\n",
      "text/latex": [
       "$$0$$"
      ],
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xa.det()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 交换\n",
    "# xo=[1,0,2,3]\n",
    "# xa,xb=xa[xo,:],xb[xo,:]\n",
    "\n",
    "# 乘k\n",
    "# xp,xk=2,1/2\n",
    "# xa[xp,:],xb[xp,:]=xa[xp,:]*xk,xb[xp,:]*xk\n",
    "\n",
    "# +乘k\n",
    "# xp,xq,xk=1,2,-1\n",
    "# xa[xp,:],xb[xp,:]=xa[xp,:]+xa[xq,:]*xk,xb[xp,:]+xb[xq,:]*xk"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}x_{0} + x_{1} - 2 x_{2} + x_{3}\\\\2 x_{0} - x_{1} - x_{2} + x_{3}\\\\2.0 x_{0} - 3.0 x_{1} + 1.0 x_{2} - 1.0 x_{3}\\\\3 x_{0} + 6 x_{1} - 9 x_{2} + 7 x_{3}\\end{matrix}\\right] = \\left[\\begin{matrix}4\\\\2\\\\2.0\\\\9\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡       x_{0} + x_{1} - 2⋅x_{2} + x_{3}       ⎤   ⎡ 4 ⎤\n",
       "⎢                                             ⎥   ⎢   ⎥\n",
       "⎢       2⋅x_{0} - x_{1} - x_{2} + x_{3}       ⎥   ⎢ 2 ⎥\n",
       "⎢                                             ⎥ = ⎢   ⎥\n",
       "⎢2.0⋅x_{0} - 3.0⋅x_{1} + 1.0⋅x_{2} - 1.0⋅x_{3}⎥   ⎢2.0⎥\n",
       "⎢                                             ⎥   ⎢   ⎥\n",
       "⎣    3⋅x_{0} + 6⋅x_{1} - 9⋅x_{2} + 7⋅x_{3}    ⎦   ⎣ 9 ⎦"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 交换\n",
    "xo=[1,0,2,3]\n",
    "xa,xb=xa[xo,:],xb[xo,:]\n",
    "\n",
    "# 乘k\n",
    "xp,xk=2,1/2\n",
    "xa[xp,:],xb[xp,:]=xa[xp,:]*xk,xb[xp,:]*xk\n",
    "\n",
    "# +乘k\n",
    "# none\n",
    "\n",
    "# show\n",
    "sp.Eq(xa*xx,xb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}x_{0} + x_{1} - 2 x_{2} + x_{3}\\\\2.0 x_{1} - 2.0 x_{2} + 2.0 x_{3}\\\\- 5.0 x_{1} + 5.0 x_{2} - 3.0 x_{3}\\\\3 x_{1} - 3 x_{2} + 4 x_{3}\\end{matrix}\\right] = \\left[\\begin{matrix}4\\\\0\\\\-6.0\\\\-3\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡ x_{0} + x_{1} - 2⋅x_{2} + x_{3}  ⎤   ⎡ 4  ⎤\n",
       "⎢                                  ⎥   ⎢    ⎥\n",
       "⎢2.0⋅x_{1} - 2.0⋅x_{2} + 2.0⋅x_{3} ⎥   ⎢ 0  ⎥\n",
       "⎢                                  ⎥ = ⎢    ⎥\n",
       "⎢-5.0⋅x_{1} + 5.0⋅x_{2} - 3.0⋅x_{3}⎥   ⎢-6.0⎥\n",
       "⎢                                  ⎥   ⎢    ⎥\n",
       "⎣   3⋅x_{1} - 3⋅x_{2} + 4⋅x_{3}    ⎦   ⎣ -3 ⎦"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 交换\n",
    "# none\n",
    "\n",
    "# 乘k\n",
    "# none\n",
    "\n",
    "# +乘k\n",
    "xp,xq,xk=1,2,-1\n",
    "xa[xp,:],xb[xp,:]=xa[xp,:]+xa[xq,:]*xk,xb[xp,:]+xb[xq,:]*xk\n",
    "\n",
    "xp,xq,xk=2,0,-2\n",
    "xa[xp,:],xb[xp,:]=xa[xp,:]+xa[xq,:]*xk,xb[xp,:]+xb[xq,:]*xk\n",
    "\n",
    "xp,xq,xk=3,0,-3\n",
    "xa[xp,:],xb[xp,:]=xa[xp,:]+xa[xq,:]*xk,xb[xp,:]+xb[xq,:]*xk\n",
    "\n",
    "# show\n",
    "sp.Eq(xa*xx,xb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}x_{0} + x_{1} - 2 x_{2} + x_{3}\\\\1.0 x_{1} - 1.0 x_{2} + 1.0 x_{3}\\\\2.0 x_{3}\\\\1.0 x_{3}\\end{matrix}\\right] = \\left[\\begin{matrix}4\\\\0\\\\-6.0\\\\-3\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡ x_{0} + x_{1} - 2⋅x_{2} + x_{3} ⎤   ⎡ 4  ⎤\n",
       "⎢                                 ⎥   ⎢    ⎥\n",
       "⎢1.0⋅x_{1} - 1.0⋅x_{2} + 1.0⋅x_{3}⎥   ⎢ 0  ⎥\n",
       "⎢                                 ⎥ = ⎢    ⎥\n",
       "⎢            2.0⋅x_{3}            ⎥   ⎢-6.0⎥\n",
       "⎢                                 ⎥   ⎢    ⎥\n",
       "⎣            1.0⋅x_{3}            ⎦   ⎣ -3 ⎦"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 交换\n",
    "# xo=[1,0,2,3]\n",
    "# xa,xb=xa[xo,:],xb[xo,:]\n",
    "\n",
    "# 乘k\n",
    "xp,xk=1,1/2\n",
    "xa[xp,:],xb[xp,:]=xa[xp,:]*xk,xb[xp,:]*xk\n",
    "\n",
    "# +乘k\n",
    "xp,xq,xk=2,1,5\n",
    "xa[xp,:],xb[xp,:]=xa[xp,:]+xa[xq,:]*xk,xb[xp,:]+xb[xq,:]*xk\n",
    "\n",
    "xp,xq,xk=3,1,-3\n",
    "xa[xp,:],xb[xp,:]=xa[xp,:]+xa[xq,:]*xk,xb[xp,:]+xb[xq,:]*xk\n",
    "\n",
    "# show\n",
    "sp.Eq(xa*xx,xb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}x_{0} + x_{1} - 2 x_{2} + x_{3}\\\\1.0 x_{1} - 1.0 x_{2} + 1.0 x_{3}\\\\1.0 x_{3}\\\\0\\end{matrix}\\right] = \\left[\\begin{matrix}4\\\\0\\\\-3\\\\0\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡ x_{0} + x_{1} - 2⋅x_{2} + x_{3} ⎤   ⎡4 ⎤\n",
       "⎢                                 ⎥   ⎢  ⎥\n",
       "⎢1.0⋅x_{1} - 1.0⋅x_{2} + 1.0⋅x_{3}⎥   ⎢0 ⎥\n",
       "⎢                                 ⎥ = ⎢  ⎥\n",
       "⎢            1.0⋅x_{3}            ⎥   ⎢-3⎥\n",
       "⎢                                 ⎥   ⎢  ⎥\n",
       "⎣                0                ⎦   ⎣0 ⎦"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 交换\n",
    "xo=[0,1,3,2]\n",
    "xa,xb=xa[xo,:],xb[xo,:]\n",
    "\n",
    "# 乘k\n",
    "# xp,xk=2,1/2\n",
    "# xa[xp,:],xb[xp,:]=xa[xp,:]*xk,xb[xp,:]*xk\n",
    "\n",
    "# +乘k\n",
    "xp,xq,xk=3,2,-2\n",
    "xa[xp,:],xb[xp,:]=xa[xp,:]+xa[xq,:]*xk,xb[xp,:]+xb[xq,:]*xk\n",
    "\n",
    "# show\n",
    "sp.Eq(xa*xx,xb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "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\n",
      "text/latex": [
       "$$\\left \\{ x_{0} : x_{2} + 4.0, \\quad x_{1} : x_{2} + 3.0, \\quad x_{3} : -3.0\\right \\}$$"
      ],
      "text/plain": [
       "{x_{0}: x_{2} + 4.0, x_{1}: x_{2} + 3.0, x_{3}: -3.0}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp.solve(sp.Eq(xa*xx,xb),xx)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**三种变换:**\n",
    ">* 交换方程的次序,记作$\\overline i\\leftrightarrow\\overline j$;\n",
    "* 以非零常数$k$乘某个方程,记作$\\overline i\\times k$;\n",
    "* 一个方程加上另一个方程的$k$倍,记作$\\overline i+k\\overline j$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**其逆变换是:**\n",
    ">* $\\overline i\\leftrightarrow\\overline j\\Rightarrow\\overline i\\leftrightarrow\\overline j$;\n",
    "* $\\overline i\\times k\\Rightarrow\\overline i\\div k$;\n",
    "* $\\overline i+k\\overline j\\Rightarrow\\overline i-k\\overline j$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**结论:**\n",
    ">1. 由于对原线性方程组施行变换是可逆变换,因此变换前后的方程组同解.\n",
    "2. 在上述线性变换过程中,实际上只对方程组的系数和常数进行运算,未知数并未参与运算."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**定义:**下列三种变换称为矩阵的**初等行变换**:\n",
    ">* 对调两行,记作$r_i\\leftrightarrow r_j$;\n",
    "* 以非零常数$k$乘某一行的所有元素,记作$r_i\\times k$;\n",
    "* 某一行加上另一行的$k$倍,记作$r_i+kr_j$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**其逆变换是:**\n",
    ">* $r_i\\leftrightarrow r_ j\\Rightarrow r_ i\\leftrightarrow r_ j$;\n",
    "* $r_ i\\times k\\Rightarrow r_ i\\div k$;\n",
    "* $r_ i+kr_ j\\Rightarrow r_ i-kr_ j$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "把上述定义中的\"行\"换成\"列\",就得到矩阵的**初等列变换**的定义.矩阵的初等行变换与初等列变换统称为**初等变换**."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "对原线性方程组施行的变换可以转化为对**增广矩阵**的变换."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}2 & -1 & -1 & 1 & 2\\\\1 & 1 & -2 & 1 & 4\\\\4 & -6 & 2 & -2 & 4\\\\3 & 6 & -9 & 7 & 9\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡2  -1  -1  1   2⎤\n",
       "⎢                ⎥\n",
       "⎢1  1   -2  1   4⎥\n",
       "⎢                ⎥\n",
       "⎢4  -6  2   -2  4⎥\n",
       "⎢                ⎥\n",
       "⎣3  6   -9  7   9⎦"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xa=sp.Matrix([[2,-1,-1,1],[1,1,-2,1],[4,-6,2,-2],[3,6,-9,7]])\n",
    "xb=sp.Matrix([2,4,4,9])\n",
    "xc=sp.Matrix.row_join(xa,xb)\n",
    "xc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}1 & 1 & -2 & 1 & 4\\\\2 & -1 & -1 & 1 & 2\\\\2.0 & -3.0 & 1.0 & -1.0 & 2.0\\\\3 & 6 & -9 & 7 & 9\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡ 1    1    -2    1     4 ⎤\n",
       "⎢                         ⎥\n",
       "⎢ 2    -1   -1    1     2 ⎥\n",
       "⎢                         ⎥\n",
       "⎢2.0  -3.0  1.0  -1.0  2.0⎥\n",
       "⎢                         ⎥\n",
       "⎣ 3    6    -9    7     9 ⎦"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 交换\n",
    "xo=[1,0,2,3]\n",
    "xc=xc[xo,:]\n",
    "\n",
    "# 乘k\n",
    "xp,xk=2,1/2\n",
    "xc[xp,:]=xc[xp,:]*xk\n",
    "\n",
    "# +乘k\n",
    "# xp,xq,xk=1,2,-1\n",
    "# xc[xp,:]=xc[xp,:]+xc[xq,:]*xk\n",
    "\n",
    "# show\n",
    "xc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}1 & 1 & -2 & 1 & 4\\\\0 & 2.0 & -2.0 & 2.0 & 0\\\\0 & -5.0 & 5.0 & -3.0 & -6.0\\\\0 & 3 & -3 & 4 & -3\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡1   1     -2    1     4  ⎤\n",
       "⎢                         ⎥\n",
       "⎢0  2.0   -2.0  2.0    0  ⎥\n",
       "⎢                         ⎥\n",
       "⎢0  -5.0  5.0   -3.0  -6.0⎥\n",
       "⎢                         ⎥\n",
       "⎣0   3     -3    4     -3 ⎦"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 交换\n",
    "# xo=[1,0,2,3]\n",
    "# xc=xc[xo,:]\n",
    "\n",
    "# 乘k\n",
    "# xp,xk=2,1/2\n",
    "# xc[xp,:]=xc[xp,:]*xk\n",
    "\n",
    "# +乘k\n",
    "xp,xq,xk=1,2,-1\n",
    "xc[xp,:]=xc[xp,:]+xc[xq,:]*xk\n",
    "\n",
    "xp,xq,xk=2,0,-2\n",
    "xc[xp,:]=xc[xp,:]+xc[xq,:]*xk\n",
    "\n",
    "xp,xq,xk=3,0,-3\n",
    "xc[xp,:]=xc[xp,:]+xc[xq,:]*xk\n",
    "\n",
    "# show\n",
    "xc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}1 & 1 & -2 & 1 & 4\\\\0 & 1.0 & -1.0 & 1.0 & 0\\\\0 & 0 & 0 & 2.0 & -6.0\\\\0 & 0 & 0 & 1.0 & -3\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡1   1    -2    1    4  ⎤\n",
       "⎢                       ⎥\n",
       "⎢0  1.0  -1.0  1.0   0  ⎥\n",
       "⎢                       ⎥\n",
       "⎢0   0    0    2.0  -6.0⎥\n",
       "⎢                       ⎥\n",
       "⎣0   0    0    1.0   -3 ⎦"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 交换\n",
    "# xo=[1,0,2,3]\n",
    "# xc=xc[xo,:]\n",
    "\n",
    "# 乘k\n",
    "xp,xk=1,1/2\n",
    "xc[xp,:]=xc[xp,:]*xk\n",
    "\n",
    "# +乘k\n",
    "xp,xq,xk=2,1,5\n",
    "xc[xp,:]=xc[xp,:]+xc[xq,:]*xk\n",
    "\n",
    "xp,xq,xk=3,1,-3\n",
    "xc[xp,:]=xc[xp,:]+xc[xq,:]*xk\n",
    "\n",
    "# show\n",
    "xc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}1 & 1 & -2 & 1 & 4\\\\0 & 1.0 & -1.0 & 1.0 & 0\\\\0 & 0 & 0 & 1.0 & -3\\\\0 & 0 & 0 & 0 & 0\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡1   1    -2    1   4 ⎤\n",
       "⎢                     ⎥\n",
       "⎢0  1.0  -1.0  1.0  0 ⎥\n",
       "⎢                     ⎥\n",
       "⎢0   0    0    1.0  -3⎥\n",
       "⎢                     ⎥\n",
       "⎣0   0    0     0   0 ⎦"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 交换\n",
    "xo=[0,1,3,2]\n",
    "xc=xc[xo,:]\n",
    "\n",
    "# 乘k\n",
    "# xp,xk=1,1/2\n",
    "# xc[xp,:]=xc[xp,:]*xk\n",
    "\n",
    "# +乘k\n",
    "xp,xq,xk=3,2,-2\n",
    "xc[xp,:]=xc[xp,:]+xc[xq,:]*xk\n",
    "\n",
    "# show\n",
    "xc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}1 & 0 & -1.0 & 0 & 4\\\\0 & 1.0 & -1.0 & 0 & 3\\\\0 & 0 & 0 & 1.0 & -3\\\\0 & 0 & 0 & 0 & 0\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡1   0   -1.0   0   4 ⎤\n",
       "⎢                     ⎥\n",
       "⎢0  1.0  -1.0   0   3 ⎥\n",
       "⎢                     ⎥\n",
       "⎢0   0    0    1.0  -3⎥\n",
       "⎢                     ⎥\n",
       "⎣0   0    0     0   0 ⎦"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 交换\n",
    "# xo=[0,1,3,2]\n",
    "# xc=xc[xo,:]\n",
    "\n",
    "# 乘k\n",
    "# xp,xk=1,1/2\n",
    "# xc[xp,:]=xc[xp,:]*xk\n",
    "\n",
    "# +乘k\n",
    "xp,xq,xk=0,1,-1\n",
    "xc[xp,:]=xc[xp,:]+xc[xq,:]*xk\n",
    "\n",
    "xp,xq,xk=1,2,-1\n",
    "xc[xp,:]=xc[xp,:]+xc[xq,:]*xk\n",
    "\n",
    "# show\n",
    "xc"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "上面**增广矩阵**对应的线性方程组为:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}x_{0} - 1.0 x_{2}\\\\1.0 x_{1} - 1.0 x_{2}\\\\1.0 x_{3}\\\\0\\end{matrix}\\right] = \\left[\\begin{matrix}4\\\\3\\\\-3\\\\0\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡  x_{0} - 1.0⋅x_{2}  ⎤   ⎡4 ⎤\n",
       "⎢                     ⎥   ⎢  ⎥\n",
       "⎢1.0⋅x_{1} - 1.0⋅x_{2}⎥   ⎢3 ⎥\n",
       "⎢                     ⎥ = ⎢  ⎥\n",
       "⎢      1.0⋅x_{3}      ⎥   ⎢-3⎥\n",
       "⎢                     ⎥   ⎢  ⎥\n",
       "⎣          0          ⎦   ⎣0 ⎦"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "expr=sp.Eq(xc[:,:-1]*xx,xc[:,-1])\n",
    "expr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "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\n",
      "text/latex": [
       "$$\\left \\{ x_{0} : x_{2} + 4.0, \\quad x_{1} : x_{2} + 3.0, \\quad x_{3} : -3.0\\right \\}$$"
      ],
      "text/plain": [
       "{x_{0}: x_{2} + 4.0, x_{1}: x_{2} + 3.0, x_{3}: -3.0}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp.solve(expr)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**备注**：\n",
    "1. 带有运算符的矩阵运算,用\"=\".例如:\n",
    "> * 矩阵加法$+$\n",
    " * 数乘矩阵、矩阵乘法$\\times$\n",
    " * 矩阵的转置$T$\n",
    " * 方阵的行列式$|\\cdot|$\n",
    "2. 不带运算符的矩阵运算,用\"~\".例如:\n",
    "> * 初等行变换\n",
    " * 初等列变换"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.2.矩阵之间的等价关系\n",
    "$\\LARGE A\\xrightarrow[有限次初等列变换]{有限次初等行变换}B,A\\overset{有限次初等变换}\\rightleftharpoons B$:\n",
    "* 行等价,记作:$\\LARGE A\\overset{r}{\\sim}B$\n",
    "* 列等价,记作:$\\LARGE A\\overset{c}{\\sim}B$\n",
    "* 矩阵等价,记作:$\\LARGE A{\\sim}B$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "矩阵之间的等价关系具有如下性质:\n",
    "> * 反身性:$A\\sim A$;\n",
    "* 对称性:若$A\\sim B,则B\\sim A$;\n",
    "* 传递性:若$A\\sim B,B\\sim C,则A\\sim C$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "前面我们在做**增广矩阵**和**线性方程组**的变换时，目的是简化为目标矩阵形式，这些目标矩阵形式如下:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**行阶梯矩阵**:\n",
    ">1. 可画出一条阶梯线,线的下方全为零;\n",
    "2. 每一个台阶都有一行;\n",
    "3. 阶梯线的竖线后面是非零行的第一个非零元素.\n",
    "\n",
    "如:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}1 & 0 & -1.0 & 0 & 4\\\\0 & 1.0 & -1.0 & 0 & 3\\\\0 & 0 & 0 & 1.0 & -3\\\\0 & 0 & 0 & 0 & 0\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡1   0   -1.0   0   4 ⎤\n",
       "⎢                     ⎥\n",
       "⎢0  1.0  -1.0   0   3 ⎥\n",
       "⎢                     ⎥\n",
       "⎢0   0    0    1.0  -3⎥\n",
       "⎢                     ⎥\n",
       "⎣0   0    0     0   0 ⎦"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xc"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**行最简形矩阵**:\n",
    "> 4. 非零行的第一个非零元为1;\n",
    "5. 这些非零元所在列的其它元素为零.\n",
    "\n",
    "如:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}1 & 0 & 0 & 0 & 0 & 0\\\\0 & 1 & 0 & 0 & 0 & 0\\\\0 & 0 & 0 & 0 & 0 & 0\\\\0 & 0 & 0 & 0 & 1 & 0\\\\0 & 0 & 0 & 0 & 0 & 1\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡1  0  0  0  0  0⎤\n",
       "⎢                ⎥\n",
       "⎢0  1  0  0  0  0⎥\n",
       "⎢                ⎥\n",
       "⎢0  0  0  0  0  0⎥\n",
       "⎢                ⎥\n",
       "⎢0  0  0  0  1  0⎥\n",
       "⎢                ⎥\n",
       "⎣0  0  0  0  0  1⎦"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xm,xn,xr=1,2,2\n",
    "sp.diag(sp.eye(xr),sp.zeros(xm,xn),sp.eye(xr))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**标准形矩阵**:\n",
    "> 6. 左上角是一个单位矩阵,其它元素全为零.\n",
    "\n",
    "**标准形矩阵**由$m,n,r$三个参数完全确定,其中$r$就是行阶梯形矩阵中非零行的行数.如:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}1 & 0 & 0 & 0 & 0\\\\0 & 1 & 0 & 0 & 0\\\\0 & 0 & 1 & 0 & 0\\\\0 & 0 & 0 & 0 & 0\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡1  0  0  0  0⎤\n",
       "⎢             ⎥\n",
       "⎢0  1  0  0  0⎥\n",
       "⎢             ⎥\n",
       "⎢0  0  1  0  0⎥\n",
       "⎢             ⎥\n",
       "⎣0  0  0  0  0⎦"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xm,xn,xr=1,2,3\n",
    "sp.diag(sp.eye(xr),sp.zeros(xm,xn))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "由上述定义可知:\n",
    ">1. **行阶梯形矩阵**包含**行最简形矩阵**;\n",
    "2. **行最简形矩阵**包含**标准形矩阵**."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**推论:**<br>\n",
    "任何矩阵$\\large\\overset{\\color{Red}{有限次初等行变换}}\\rightleftharpoons$行阶梯形矩阵$\\large\\overset{\\color{Red}{有限次初等行变换}}\\rightleftharpoons$行最简形矩阵$\\large\\overset{\\color{blue}{有限次初等列变换}}\\rightleftharpoons$标准形矩阵$\\large\\overset{\\color{magenta}{有限次初等变换}}\\rightleftharpoons$任何矩阵"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.3.初等变换与矩阵乘法的关系\n",
    "**定义**:由单位矩阵$E$经过一次初等变换得到的矩阵称为**初等矩阵**.三种初等变换对应着三种初等矩阵.\n",
    ">1. 对调单位阵的两行(列),记作$E_m(i,j)$;\n",
    "2. 以常数$k\\not=0$乘单位阵的某一行(列),记作$E_m(i(k))$;\n",
    "3. 以$k$乘单位阵的某一行(列)加到另一行(列),记作$E_m(ij(k))$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**初等变换:**\n",
    ">1. $E_m(i,j)A_{m\\times n}$,即,把矩阵$A$的第$i$行与第$j$行对调;\n",
    "2. $A_{m\\times n}E_n(i,j)$,即,把矩阵$A$的第$i$列与第$j$列对调;\n",
    "3. $E_m(i(k))A_{m\\times n}$,即,以非零常数$k\\not=0$乘矩阵$A$的第$i$行;\n",
    "4. $A_{m\\times n}E_n(i(k))$,即,以非零常数$k\\not=0$乘矩阵$A$的第$i$列;\n",
    "5. $E_m(ij(k))A_{m\\times n}$,即,把矩阵$A$第$j$行的$k$倍加到第$i$行;\n",
    "6. $A_{m\\times n}E_n(ij(k))$,即,把矩阵$A$第$j$列的$k$倍加到第$i$列;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**性质1:**矩阵$A\\in\\mathbb R^{m\\times n}$,\n",
    "1. 对$A$施行一次**初等行变换**,相当于在**$A$的左边**乘以相应的$m$阶初等矩阵;\n",
    "2. 对$A$施行一次**初等列变换**,相当于在**$A$的右边**乘以相应的$n$阶初等矩阵."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left ( \\left[\\begin{matrix}a_{{0}{0}} & a_{{0}{1}} & a_{{0}{2}} & a_{{0}{3}}\\\\a_{{1}{0}} & a_{{1}{1}} & a_{{1}{2}} & a_{{1}{3}}\\\\a_{{2}{0}} & a_{{2}{1}} & a_{{2}{2}} & a_{{2}{3}}\\\\a_{{3}{0}} & a_{{3}{1}} & a_{{3}{2}} & a_{{3}{3}}\\end{matrix}\\right], \\quad \\left[\\begin{matrix}1 & 0 & 0 & 0\\\\0 & 1 & 0 & 0\\\\0 & 0 & 1 & 0\\\\0 & 0 & 0 & 1\\end{matrix}\\right]\\right )$$"
      ],
      "text/plain": [
       "⎛⎡a_{{0}{0}}  a_{{0}{1}}  a_{{0}{2}}  a_{{0}{3}}⎤  ⎡1  0  0  0⎤⎞\n",
       "⎜⎢                                              ⎥  ⎢          ⎥⎟\n",
       "⎜⎢a_{{1}{0}}  a_{{1}{1}}  a_{{1}{2}}  a_{{1}{3}}⎥  ⎢0  1  0  0⎥⎟\n",
       "⎜⎢                                              ⎥, ⎢          ⎥⎟\n",
       "⎜⎢a_{{2}{0}}  a_{{2}{1}}  a_{{2}{2}}  a_{{2}{3}}⎥  ⎢0  0  1  0⎥⎟\n",
       "⎜⎢                                              ⎥  ⎢          ⎥⎟\n",
       "⎝⎣a_{{3}{0}}  a_{{3}{1}}  a_{{3}{2}}  a_{{3}{3}}⎦  ⎣0  0  0  1⎦⎠"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xn=4\n",
    "xa=getM('a',xn,xn)\n",
    "xe=sp.eye(xn)\n",
    "xa,xe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left ( \\left[\\begin{matrix}a_{{1}{0}} & a_{{1}{1}} & a_{{1}{2}} & a_{{1}{3}}\\\\a_{{0}{0}} & a_{{0}{1}} & a_{{0}{2}} & a_{{0}{3}}\\\\a_{{2}{0}} & a_{{2}{1}} & a_{{2}{2}} & a_{{2}{3}}\\\\a_{{3}{0}} & a_{{3}{1}} & a_{{3}{2}} & a_{{3}{3}}\\end{matrix}\\right], \\quad \\left[\\begin{matrix}a_{{0}{1}} & a_{{0}{0}} & a_{{0}{2}} & a_{{0}{3}}\\\\a_{{1}{1}} & a_{{1}{0}} & a_{{1}{2}} & a_{{1}{3}}\\\\a_{{2}{1}} & a_{{2}{0}} & a_{{2}{2}} & a_{{2}{3}}\\\\a_{{3}{1}} & a_{{3}{0}} & a_{{3}{2}} & a_{{3}{3}}\\end{matrix}\\right]\\right )$$"
      ],
      "text/plain": [
       "⎛⎡a_{{1}{0}}  a_{{1}{1}}  a_{{1}{2}}  a_{{1}{3}}⎤  ⎡a_{{0}{1}}  a_{{0}{0}}  a_\n",
       "⎜⎢                                              ⎥  ⎢                          \n",
       "⎜⎢a_{{0}{0}}  a_{{0}{1}}  a_{{0}{2}}  a_{{0}{3}}⎥  ⎢a_{{1}{1}}  a_{{1}{0}}  a_\n",
       "⎜⎢                                              ⎥, ⎢                          \n",
       "⎜⎢a_{{2}{0}}  a_{{2}{1}}  a_{{2}{2}}  a_{{2}{3}}⎥  ⎢a_{{2}{1}}  a_{{2}{0}}  a_\n",
       "⎜⎢                                              ⎥  ⎢                          \n",
       "⎝⎣a_{{3}{0}}  a_{{3}{1}}  a_{{3}{2}}  a_{{3}{3}}⎦  ⎣a_{{3}{1}}  a_{{3}{0}}  a_\n",
       "\n",
       "{{0}{2}}  a_{{0}{3}}⎤⎞\n",
       "                    ⎥⎟\n",
       "{{1}{2}}  a_{{1}{3}}⎥⎟\n",
       "                    ⎥⎟\n",
       "{{2}{2}}  a_{{2}{3}}⎥⎟\n",
       "                    ⎥⎟\n",
       "{{3}{2}}  a_{{3}{3}}⎦⎠"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xe[[1,0,2,3],:]*xa,xa*xe[[1,0,2,3],:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}a_{{0}{0}} + 2 a_{{2}{0}} & a_{{0}{1}} + 2 a_{{2}{1}} & a_{{0}{2}} + 2 a_{{2}{2}} & a_{{0}{3}} + 2 a_{{2}{3}}\\\\a_{{1}{0}} & a_{{1}{1}} & a_{{1}{2}} & a_{{1}{3}}\\\\a_{{2}{0}} & a_{{2}{1}} & a_{{2}{2}} & a_{{2}{3}}\\\\a_{{3}{0}} & a_{{3}{1}} & a_{{3}{2}} & a_{{3}{3}}\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡a_{{0}{0}} + 2⋅a_{{2}{0}}  a_{{0}{1}} + 2⋅a_{{2}{1}}  a_{{0}{2}} + 2⋅a_{{2}{2\n",
       "⎢                                                                             \n",
       "⎢       a_{{1}{0}}                 a_{{1}{1}}                 a_{{1}{2}}      \n",
       "⎢                                                                             \n",
       "⎢       a_{{2}{0}}                 a_{{2}{1}}                 a_{{2}{2}}      \n",
       "⎢                                                                             \n",
       "⎣       a_{{3}{0}}                 a_{{3}{1}}                 a_{{3}{2}}      \n",
       "\n",
       "}}  a_{{0}{3}} + 2⋅a_{{2}{3}}⎤\n",
       "                             ⎥\n",
       "           a_{{1}{3}}        ⎥\n",
       "                             ⎥\n",
       "           a_{{2}{3}}        ⎥\n",
       "                             ⎥\n",
       "           a_{{3}{3}}        ⎦"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xe[0,:]=xe[0,:]+xe[2,:]*2\n",
    "xe*xa"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**推论:**根据逆矩阵的定义:对于$n$阶方阵$A,B$,如果$AB=E$,那么$A,B$都是可逆矩阵,并且他们互为逆矩阵,<br>\n",
    "又因为:\n",
    "1. 根据**反身性**性质,矩阵的逆变换为其自身;\n",
    "2. **初等矩阵**与单位矩阵$E$相乘为其自身;\n",
    "\n",
    "那么:<br>\n",
    "$\\large E_m(i,j)E_m(i,j)E_m=E_m(i,j)E_m(i,j)=E_m\\to E_m(i,j)={E_m(i,j)}^{-1}$<br>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}1 & 0 & 0\\\\0 & 1 & 0\\\\0 & 0 & 1\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡1  0  0⎤\n",
       "⎢       ⎥\n",
       "⎢0  1  0⎥\n",
       "⎢       ⎥\n",
       "⎣0  0  1⎦"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xn=3\n",
    "xe=sp.eye(xn)\n",
    "xe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xa=xe.copy()\n",
    "xa[::-1,:]==xa[::-1,:].inv()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**推论:**<br>\n",
    "$\\large E_m(i(\\frac 1 k))E_m(i(k))E_m=E_m(i(\\frac 1 k))E_m(i(k))=E_m\\to {E_m(i(k))}^{-1}=E_m(i(\\frac 1 k))$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xa=xe.copy()\n",
    "xa[1,:]=xa[1,:]*xn\n",
    "xb=xe.copy()\n",
    "xb[1,:]=xb[1,:]/xn\n",
    "xa.inv()==xb"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**推论:**<br>\n",
    "$\\large E_mE_m(ij(k))E_m(ij(-k))=E_m(ij(k))E_m(ij(-k))=E_m\\to{E_m(ij(k))}^{-1}=E_m(ij(-k))$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xa=xe.copy()\n",
    "xa[1,:]=xa[1,:]+xa[2,:]*xn\n",
    "xb=xe.copy()\n",
    "xb[1,:]=xb[1,:]-xb[2,:]*xn\n",
    "xa.inv()==xb"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**性质2:**方阵$A$可逆的充要条件是存在有限个初等矩阵$P_1,\\dots,P_l$,使$A=P_1\\dots P_l$.<br>\n",
    "这表明,可逆矩阵的标准形矩阵是单位阵.其实,可逆矩阵的行最简形矩阵也是单位阵."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**推论1:**方阵$A$可逆的充要条件是$A\\overset r \\sim E$.<br>\n",
    "**推论2:**方阵$A与B$等价的充要条件是存在$m$阶可逆矩阵$P$及$n$阶可逆矩阵$Q$,使$PAQ=B$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.4.初等变换的应用\n",
    "当$|A|\\not=0$时,由$A=P_1\\dots P_l$,有${P_l}^{-1}\\dots {P_1}^{-1}A=E$,及${P_l}^{-1}\\dots {P_1}^{-1}E=A^{-1}$,<br>\n",
    "$\\therefore {P_l}^{-1}\\dots {P_1}^{-1}(A|E)=({P_l}^{-1}\\dots {P-1}^{-1}A|{P_l}^{-1}\\dots {P_1}^{-1}E)=(E|A^{-1})$<br>\n",
    "即对$n\\times 2n$矩阵$(A|E)$施行初等行变换,当把$A变成E$时,原来的$E$就变成$A^{-1}$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "利用初等行变换求逆矩阵的方法,还可用于求矩阵$A^{-1}B$.<br>\n",
    "$\\because A^{-1}(A|B)=(E|A^{-1}B)$<br>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果要求$CA^{-1}$,则可对矩阵$\\begin{bmatrix}A\\\\C\\end{bmatrix}$做初等列变换,$\\begin{bmatrix}A\\\\C\\end{bmatrix}\\overset{列变换}\\sim\\begin{bmatrix}E\\\\CA^{-1}\\end{bmatrix},即可得到CA^{-1}$.<br>\n",
    "也可改为对$(A^T,C^T)\\overset{行变换}\\sim (E,{(A^T)}^{-1}C^T)$,即可得到${(A^T)}^{-1}C^T)$,接着对其转置,即可得到结果$CA^{-1}$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2.矩阵的秩\n",
    "## 2.1.矩阵的秩的概念\n",
    "**定义:**在$m\\times n矩阵A$中,任取$k行k列(k\\leq m,n)$,位于这些行列交叉处的$k^2$个元素,不改变它们在$A$中所处的位置次序而得的$k$阶行列式,称为矩阵$A$的**$k$阶子式**.\n",
    "\n",
    "显然,$m\\times n$矩阵$A$的k阶子式共有$C_m^kC_n^k$个."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**定义:**设矩阵$A$中有一个不等于零的$r$阶子式$D$,且所有$r+1$阶子式(如果存在的话)全等于零,那么$D$称为矩阵$A$的**最高阶非零子式**,数$r$称为**矩阵$A$的秩**,记作$R(A)$.**规定:**零矩阵的秩等于零.\n",
    "> * 根据行列式按行(列)展开法则可知,矩阵$A$中任何一个$r+2$阶子式(如果存在的话)都可以用$r+1$阶子式来表示.\n",
    "* 如果矩阵$A$中所有$r+1$阶子式都等于零,那么所有$r+2$阶子式也都等于零.\n",
    "* 事实上,所有高于$r+1$阶的子式(如果存在的话)也都等于零.\n",
    "\n",
    "因此,矩阵$A$的秩就是$A$中非零子式的最高阶数."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "显然,\n",
    ">* 若矩阵$A$中有某个$s$阶子式不等于零,则$R(A)\\geq s$;若矩阵$A$中所有$t$阶子式等于零,则$R(A)<t$.\n",
    "* 若矩阵$A$为$n$阶矩阵,则$A$的$n$阶子式只有一个,即$|A|$.\n",
    "    * 当$|A|\\not=0$时，$R(A)=n$;可逆矩阵(非奇异矩阵)又称为**满秩矩阵**.\n",
    "    * 当$|A|=0$时,$R(A)<n$;不可逆矩阵(奇异矩阵)又称为**降秩矩阵**.\n",
    "* 若$A$为$m\\times n$矩阵,则$0\\leq R(A)\\leq min(m,n)$.\n",
    "* $R(A^T)=R(A)$.\n",
    "\n",
    "**结论:**行阶梯形矩阵的秩就等于非零行的行数."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.2.矩阵的秩的计算\n",
    "一般的矩阵,当行数和列数较高时,按定义求秩是很麻烦的.$\\Rightarrow $行阶梯形矩阵的秩就等于非零行的行数.$\\Rightarrow $自然可以用初等变换将一般的矩阵化为行阶梯形矩阵."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**定理:**若$A\\sim B$,则$R(A)=R(B)$.\n",
    "\n",
    "**论证:**\n",
    ">1. $A$经过一次初等行变换变为$B$,则$R(A)\\leq R(B)$. \n",
    "2. $B$也可经由一次初等行变换变为$A$,则$R(B)\\leq R(A)$,于是$R(A)=R(B)$.\n",
    "3. 经过一次初等行变换的矩阵的秩不变,经过有限次初等行变换的矩阵的秩仍然不变.\n",
    "4. 设$A$经过**初等列变换**变为$B$,则$A^T$经过**初等行变换**变为$B^T$,从而$R(A^T)=R(B^T)$.又$R(A)=R(A^T),R(B)=R(B^T),因此R(A)=R(B)$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**应用:**根据上面的定理,求矩阵的秩,只要用初等行变换把矩阵化成行阶梯形矩阵,行阶梯形矩阵中非零行的行数就是该矩阵的秩."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "总结,**矩阵的秩的性质:**\n",
    ">1. 若$A$为$m\\times n$矩阵,则$0\\leq R(A)\\leq min(m,n)$.\n",
    "2. $R(A^T)=R(A)$.\n",
    "3. 若$A\\sim B$,则$R(A)=R(B)$.\n",
    "4. 若$P,Q$可逆,则$R(PAQ)=R(B)$.\n",
    "5. $max\\{R(A),R(B)\\}\\leq R(A,B)\\leq R(A)+R(B)$.特别地,当$B=b$为非零列向量时,有$R(A)\\leq R(A,b)\\leq R(A)+1$.\n",
    "6. $R(A+B)\\leq R(A)+R(B)$.\n",
    "7. $R(AB)\\leq min\\{R(A),R(B)\\}$.\n",
    "8. 若$A_{m\\times n}B_{n\\times l}=O,则R(A)+R(B)\\leq n$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**例:**设$A为n$阶矩阵,证明$R(A+E)+R(A-E)\\geq n$.\n",
    "\n",
    "**证明:**<br>\n",
    "$\\because (A+E)+(E-A)=2E$,由性质$R(A+B)\\leq R(A)+R(B)$<br>\n",
    "$\\therefore R(A+E)+R(E-A)\\geq R(2E)=n.$<br>\n",
    "$\\because R(E-A)=R(A-E),$<br>\n",
    "$\\therefore R(A+E)+R(A-E)\\geq n$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**例:**若$A_{m\\times n}B_{n\\times l}=C,且R(A)=n,则R(B)=R(C)$.\n",
    "\n",
    "**证明:**<br>\n",
    ">$\\because R(A)=n$,<br>\n",
    "$\\therefore A的行最简形矩阵为{\\begin{bmatrix}E_n\\\\O\\end{bmatrix}}_{m\\times n}$,<br>\n",
    "$设m阶可逆矩阵P$,满足$PA={\\begin{bmatrix}E_n\\\\O\\end{bmatrix}}_{m\\times n}$.<br>\n",
    "$\\therefore PC=PAB={\\begin{bmatrix}E_n\\\\O\\end{bmatrix}}B=\\begin{bmatrix}B\\\\O\\end{bmatrix}$,<br>\n",
    "$\\because R(C)=R(PC),而R(B)=R\\begin{bmatrix}B\\\\O\\end{bmatrix}$,<br>\n",
    "$\\therefore R(B)=R(C)$\n",
    "\n",
    "**附注:**<br>\n",
    ">* 当一个矩阵的秩等于它的列数时,这样的矩阵称为**列满秩矩阵**.\n",
    "* 特别地,当一个矩阵为方阵时,列满秩矩阵就成为满秩矩阵,也就是可逆矩阵.\n",
    "* 上面,$当C=O$,这时结论为:设$AB=O,若A为列满秩矩阵,则B=O$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3.线性方程组的解\n",
    "## 3.1.线性方程组的表达式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left ( \\left[\\begin{matrix}a_{{0}{0}} & a_{{0}{1}} & a_{{0}{2}}\\\\a_{{1}{0}} & a_{{1}{1}} & a_{{1}{2}}\\\\a_{{2}{0}} & a_{{2}{1}} & a_{{2}{2}}\\end{matrix}\\right], \\quad \\left[\\begin{matrix}b_{0}\\\\b_{1}\\\\b_{2}\\end{matrix}\\right], \\quad \\left[\\begin{matrix}x_{0}\\\\x_{1}\\\\x_{2}\\end{matrix}\\right]\\right )$$"
      ],
      "text/plain": [
       "⎛⎡a_{{0}{0}}  a_{{0}{1}}  a_{{0}{2}}⎤  ⎡b_{0}⎤  ⎡x_{0}⎤⎞\n",
       "⎜⎢                                  ⎥  ⎢     ⎥  ⎢     ⎥⎟\n",
       "⎜⎢a_{{1}{0}}  a_{{1}{1}}  a_{{1}{2}}⎥, ⎢b_{1}⎥, ⎢x_{1}⎥⎟\n",
       "⎜⎢                                  ⎥  ⎢     ⎥  ⎢     ⎥⎟\n",
       "⎝⎣a_{{2}{0}}  a_{{2}{1}}  a_{{2}{2}}⎦  ⎣b_{2}⎦  ⎣x_{2}⎦⎠"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xn=3\n",
    "xa,xb,xx=[getM(i,j,k) for i,j,k in [('a',xn,xn),('b',xn,1),('x',xn,1)]]\n",
    "xa,xb,xx"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "一般形式=向量方程的形式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}a_{{0}{0}} x_{0} + a_{{0}{1}} x_{1} + a_{{0}{2}} x_{2}\\\\a_{{1}{0}} x_{0} + a_{{1}{1}} x_{1} + a_{{1}{2}} x_{2}\\\\a_{{2}{0}} x_{0} + a_{{2}{1}} x_{1} + a_{{2}{2}} x_{2}\\end{matrix}\\right] = \\left[\\begin{matrix}b_{0}\\\\b_{1}\\\\b_{2}\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡a_{{0}{0}}⋅x_{0} + a_{{0}{1}}⋅x_{1} + a_{{0}{2}}⋅x_{2}⎤   ⎡b_{0}⎤\n",
       "⎢                                                      ⎥   ⎢     ⎥\n",
       "⎢a_{{1}{0}}⋅x_{0} + a_{{1}{1}}⋅x_{1} + a_{{1}{2}}⋅x_{2}⎥ = ⎢b_{1}⎥\n",
       "⎢                                                      ⎥   ⎢     ⎥\n",
       "⎣a_{{2}{0}}⋅x_{0} + a_{{2}{1}}⋅x_{1} + a_{{2}{2}}⋅x_{2}⎦   ⎣b_{2}⎦"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp.Eq(xa*xx,xb)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "增广矩阵形式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}a_{{0}{0}} & a_{{0}{1}} & a_{{0}{2}} & b_{0}\\\\a_{{1}{0}} & a_{{1}{1}} & a_{{1}{2}} & b_{1}\\\\a_{{2}{0}} & a_{{2}{1}} & a_{{2}{2}} & b_{2}\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡a_{{0}{0}}  a_{{0}{1}}  a_{{0}{2}}  b_{0}⎤\n",
       "⎢                                         ⎥\n",
       "⎢a_{{1}{0}}  a_{{1}{1}}  a_{{1}{2}}  b_{1}⎥\n",
       "⎢                                         ⎥\n",
       "⎣a_{{2}{0}}  a_{{2}{1}}  a_{{2}{2}}  b_{2}⎦"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp.Matrix.row_join(xa,xb)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "向量线性组合的形式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$$\\left[\\begin{matrix}\\left[\\begin{matrix}a_{{0}{0}} x_{0} + a_{{0}{1}} x_{1} + a_{{0}{2}} x_{2}\\\\a_{{1}{0}} x_{0} + a_{{1}{1}} x_{1} + a_{{1}{2}} x_{2}\\\\a_{{2}{0}} x_{0} + a_{{2}{1}} x_{1} + a_{{2}{2}} x_{2}\\end{matrix}\\right]\\end{matrix}\\right] = \\left[\\begin{matrix}\\left[\\begin{matrix}b_{0}\\\\b_{1}\\\\b_{2}\\end{matrix}\\right]\\end{matrix}\\right]$$"
      ],
      "text/plain": [
       "⎡⎡a_{{0}{0}}⋅x_{0} + a_{{0}{1}}⋅x_{1} + a_{{0}{2}}⋅x_{2}⎤⎤   ⎡⎡b_{0}⎤⎤\n",
       "⎢⎢                                                      ⎥⎥   ⎢⎢     ⎥⎥\n",
       "⎢⎢a_{{1}{0}}⋅x_{0} + a_{{1}{1}}⋅x_{1} + a_{{1}{2}}⋅x_{2}⎥⎥ = ⎢⎢b_{1}⎥⎥\n",
       "⎢⎢                                                      ⎥⎥   ⎢⎢     ⎥⎥\n",
       "⎣⎣a_{{2}{0}}⋅x_{0} + a_{{2}{1}}⋅x_{1} + a_{{2}{2}}⋅x_{2}⎦⎦   ⎣⎣b_{2}⎦⎦"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xas=sp.Matrix([[xa[:,i]] for i in range(xn)]).T#向量组形式\n",
    "xbs=sp.Matrix([[xb,]])\n",
    "sp.Eq(xas*xx,xbs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "方程组可简化为$Ax=b$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.2.线性方程组的解的判定\n",
    "设有$n$个未知数的$m$个方程的线性方程组<br>\n",
    "$\\begin{cases} a_{11}x_{1}+\\dots+a_{1n}x_{n}=b_{1}\\\\\\dots\\\\  a_{m1}x_{1}+\\dots+a_{mn}x_{n}=b_{m}\\end{cases}$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**定义:**线性方程组如果有解,就称它是**相容的**;如果无解,就称它是**不相容的**."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "求解一个线性方程组,需要回答如下问题:\n",
    ">1. 方程组是否有解?\n",
    "2. 若方程组有解,则解是否唯一?\n",
    "3. 若方程组有解且不唯一,则如何掌握解的全体?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**定理:**$n$元线性方程组$Ax=b$\n",
    ">1. 无解的充分必要条件是$R(A)<R(A,b)$;\n",
    "2. 有唯一解的充分必要条件是$R(A)=R(A,b)=n$;\n",
    "3. 有无限多解的充分必要条件是$R(A)=R(A,b)< n$.\n",
    "\n",
    "**证明过程:略**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**推论:**齐次线性方程组$Ax=0$的常数项都等于零,于是必有$R(A,0)=R(A)$,所以可从$R(A)$判断齐次线性方程组的解的情况."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**定理:**$n$元齐次线性方程组$Ax=0$有非零解的充分必要条件是$R(A)<n$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**定理:**矩阵方程组$AX=B$有解的充分必要条件是$R(A)=R(A,B)$.\n",
    "\n",
    "**证明:**<br>\n",
    ">设$A\\in\\mathbb{R}^{m\\times n}矩阵,B\\in\\mathbb{R}^{m\\times l}矩阵,X\\in\\mathbb{R}^{n\\times l}矩阵.$<br>\n",
    "$把X和B按列分块,记作X=(x_1,\\dots,x_l),B=(b_1,\\dots,b_l)$<br>\n",
    "$则AX=A(x_1,\\dots,x_l)=(Ax_1,\\dots,Ax_l)=(b_1,\\dots,b_l)=B$,<br>\n",
    "$\\therefore 矩阵方程{\\color{red}{AX=B}}有解\\Leftrightarrow 线性方程组{\\color{red}{Ax_i=b_i}}有解\\Leftrightarrow R(A)=R(A,b_i)$,<br>\n",
    "$设R(A)=r,A的行最简形矩阵为\\tilde A,则\\tilde A有r个非零行,且\\tilde A的后m-r行全为零.$<br>\n",
    "$再设(A,B)=(A,b_1,\\dots,b_l)\\overset r \\sim (\\tilde A,\\tilde {b_1},\\dots,\\tilde {b_l})$,<br>\n",
    "$从而(A,b_i)\\overset r \\sim (\\tilde A,\\tilde {b_i})$.<br>\n",
    "$\\therefore 矩阵方程{\\color{red}{AX=B}}有解\\\\\\Leftrightarrow 线性方程组{\\color{red}{Ax_i=b_i}}有解\\\\\\Leftrightarrow R(A)=R(A,b_i)\\\\\\Leftrightarrow \\tilde {b_i}的后m-r个元素全为零\\\\\\Leftrightarrow (\\tilde {b_1},\\dots,\\tilde {b_l})后m-r个元素全为零\\\\\\Leftrightarrow R(A)=R(A,B)$\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**定理:**$设AB=C,则R(C)\\leq min\\{R(A),R(B)\\}$.\n",
    "\n",
    "**证明:**\n",
    ">$\\because AB=C,\\\\\\therefore 矩阵方程{\\color{red}{AX=C}}有解X=B,\\\\\\therefore R(A)=R(A,C).\\\\\\because R(C)\\leq R(A,C),\\\\\\therefore R(C)\\leq R(A).\\\\ 又\\because {(AB)}^T=C^T,即B^TA^T=C^T,\\\\\\therefore 矩阵方程B^TX=C^T有解X=A^T,\\\\同理可得R(C)\\leq R(B).\\\\ 综上,可知R(C)\\leq min\\{R(A),R(B)\\}$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**推论:**对于非齐次线性方程组\n",
    ">1. 无解的充分必要条件是$R(A)<R(A,B)$;\n",
    "2. 有唯一解的充分必要条件是$R(A)=R(A,B)=n$;\n",
    "3. 有无限多解的充分必要条件是$R(A)=R(A,B)<n,包含n-R(A)个自由变量的通解$."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
